package cn.iocoder.yudao.module.trade.service.ordersafetycheckterm;

import cn.iocoder.yudao.framework.common.enums.OrderCheckTypeEnum;
import cn.iocoder.yudao.framework.common.util.CheckUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.trade.controller.admin.ordersafetycheckterm.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.ordersafetycheckterm.OrderSafetyCheckTermDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.trade.dal.mysql.ordersafetycheckterm.OrderSafetyCheckTermMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*;

/**
 * 安检项 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class OrderSafetyCheckTermServiceImpl implements OrderSafetyCheckTermService {

    @Resource
    private OrderSafetyCheckTermMapper orderSafetyCheckTermMapper;

    @Override
    public Long createOrderSafetyCheckTerm(OrderSafetyCheckTermSaveReqVO createReqVO) {
        // 插入
        OrderSafetyCheckTermDO orderSafetyCheckTerm = BeanUtils.toBean(createReqVO, OrderSafetyCheckTermDO.class);
        orderSafetyCheckTermMapper.insert(orderSafetyCheckTerm);
        // 返回
        return orderSafetyCheckTerm.getId();
    }

    @Override
    public void updateOrderSafetyCheckTerm(OrderSafetyCheckTermRespVO updateReqVO) {
        // 校验存在
        validateOrderSafetyCheckTermExists(updateReqVO.getId());
        // 更新
        OrderSafetyCheckTermDO updateObj = BeanUtils.toBean(updateReqVO, OrderSafetyCheckTermDO.class);
        orderSafetyCheckTermMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderSafetyCheckTerm(Long id) {
        // 校验存在
        validateOrderSafetyCheckTermExists(id);
        // 删除
        orderSafetyCheckTermMapper.deleteById(id);
    }

    private void validateOrderSafetyCheckTermExists(Long id) {
        if (orderSafetyCheckTermMapper.selectById(id) == null) {
            throw exception(ORDER_SAFETY_CHECK_TERM_NOT_EXISTS);
        }
    }

    @Override
    public OrderSafetyCheckTermDO getOrderSafetyCheckTerm(Long id) {
        return orderSafetyCheckTermMapper.selectById(id);
    }

    @Override
    public PageResult<OrderSafetyCheckTermDO> getOrderSafetyCheckTermPage(OrderSafetyCheckTermPageReqVO pageReqVO) {
        return orderSafetyCheckTermMapper.selectPage(pageReqVO);
    }

    @Override
    public void insertBathList(List<OrderSafetyCheckTermSaveReqVO> reqVOList) {
        orderSafetyCheckTermMapper.insertOrUpdateBatch(BeanUtils.toBean(reqVOList, OrderSafetyCheckTermDO.class));
    }

    @Override
    public void updateBathList(List<OrderSafetyCheckTermRespVO> termSaveReqVOList) {
        orderSafetyCheckTermMapper.updateBatch(BeanUtils.toBean(termSaveReqVOList, OrderSafetyCheckTermDO.class));
    }

    /**
     *
     *  通过安检项标题id查询对应的明细
     * @param id
     * @return
     */
    @Override
    public List<OrderSafetyCheckTermSaveReqVO> getOrderSafetyCheckTermList(Long id) {

        LambdaQueryWrapperX<OrderSafetyCheckTermDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eqIfPresent(OrderSafetyCheckTermDO::getItemId, id)
                .eqIfPresent(OrderSafetyCheckTermDO::getStatus, 0)
                .orderByAsc(OrderSafetyCheckTermDO ::getIdIndex);

        return BeanUtils.toBean(orderSafetyCheckTermMapper.selectList(wrapperX), OrderSafetyCheckTermSaveReqVO.class);
    }

    @Override
    public void deleteBatchItemIds(List<Long> itemIds) {
        itemIds.forEach(x -> {
            LambdaQueryWrapperX<OrderSafetyCheckTermDO> wrapperX = new LambdaQueryWrapperX<>();
            wrapperX.eqIfPresent(OrderSafetyCheckTermDO::getItemId, x);
            orderSafetyCheckTermMapper.delete(wrapperX);
        });

    }

    @Override
    public void compareCheckItem(List<OrderSafetyCheckTermSaveReqVO> termSaveReqVOList) {
        List<OrderSafetyCheckTermDO> list = BeanUtils.toBean(termSaveReqVOList, OrderSafetyCheckTermDO.class);
        list.forEach(x -> {
            if (!CheckUtils.isEmpty(x.getId())) {
                OrderSafetyCheckTermDO idemDo = orderSafetyCheckTermMapper.selectById(x.getId());

                if (Objects.equals(x.getType(), OrderCheckTypeEnum.DESCRIPTION.getStatus()) && !x.getContent().equals(idemDo.getContent())) {
                    saveCheckItem(x);
                    return;
                }
                if ((Objects.equals(x.getType(), OrderCheckTypeEnum.SIGN.getStatus()) || Objects.equals(x.getType(), OrderCheckTypeEnum.VIDEO.getStatus())) && !x.getSelected().equals(idemDo.getSelected())) {
                    saveCheckItem(x);
                    return;
                }
                if (Objects.equals(x.getType(), OrderCheckTypeEnum.PHOTO.getStatus()) && !x.getOptions().equals(idemDo.getOptions())) {
                    saveCheckItem(x);
                    return;
                }
                if (StringUtils.isNotEmpty(x.getOptions()) && !x.getOptions().equals(idemDo.getOptions())){
                    saveCheckItem(x);
                    return;
                }
                orderSafetyCheckTermMapper.updateById(x);
            } else {
                orderSafetyCheckTermMapper.insert(x);
            }

        });
    }

    private void saveCheckItem(OrderSafetyCheckTermDO x) {
        orderSafetyCheckTermMapper.updateById(new OrderSafetyCheckTermDO().setId(x.getId()).setStatus(1));
        x.setId(null);
        orderSafetyCheckTermMapper.insert(x);
    }
}